<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * A HMVC Template Library
 * 
 * @author Leon Rowland
 * @version 2.0
 */
class Template
{

	// Hold the CI Instance
	private $_ci;
	// Holds the template
	private $_template;
	// Holds the templates folder
	private $_template_folder;
	// Holds the layout
	private $_layout;
	// Holds the layouts folder
	private $_layouts_folder;
	// Partials
	private $_partials = array();
	// Holds the partials folder
	private $_partials_folder;
	// Hold the data for the view
	private $_data = array();
	// Holds data for the layout
	private $_layout_data = array();
	// Holds the module name
	private $_module;
	// Holds the controller name
	private $_controller;
	// Holds the function name
	private $_method;
	// Holds the View Line
	private $_view;

	/**
	 * Constructor
	 * 
	 * Gets the CI super and sets default values
	 */
	public function __construct()
	{
		// Set CI Instance
		$this->_ci = & get_instance();
		
		// Sets the default values
		$this->_template = 'default';
		$this->_template_folder = 'templates';
		$this->_layout = 'layout';
		$this->_layouts_folder = 'layouts';
		$this->_partials_folder = 'partials';

		// Finds the called URI Segments
		$this->_module = $this->_ci->router->fetch_module();
		$this->_controller = $this->_ci->router->fetch_class();
		$this->_method = $this->_ci->router->fetch_method();
		
		// Set the view file assumption
		//$this->_view = "{$this->_module}/{$this->_template}/{$this->_controller}-{$this->_method}";
		$this->_view = "{$this->_module}/{$this->_controller}_{$this->_method}";
	}

	/**
	 * Sets the title for the page
	 * 
	 * @access public
	 * @param string $page_title 
	 * @return Template
	 */
	public function title($page_title)
	{
		echo $page_title;die;
		// Set the title and return class
		$this->_data['title'] = $page_title;
		return $this;
	}

	/**
	 * Sets data for a view
	 * 
	 * @access public
	 * @param string $key | The variable name in the view
	 * @param string $value | The value of it
	 * @return Template 
	 */
	public function set($key, $value)
	{
		// Adds data to be sent to view
		$this->_data[$key] = $value;
		return $this;
	}

	/**
	 * Adds an asset reference
	 * 
	 * @access public
	 * @param string $file
	 * @return Template
	 */
	public function asset($file, $ext = NULL)
	{
		// Find extension
		$ext = (!$ext) ? end(explode('.', $file)) : $ext;

		// Make the HTML
		switch($ext)
		{
			case 'css':
				$this->_data['assets']['css'][] = "<link rel='stylesheet' href='$file' type='text/css'/>";
				break;
			case 'js':
				$this->_data['assets']['js'][] = "<script type='text/javascript' src='$file'></script>";
				break;
		}

		return $this;
	}

	/**
	 * Adds a full line to the meta data
	 * 
	 * @access public
	 * @param string $line
	 * @return Template 
	 */
	public function meta_line($line)
	{
		$this->_data['metadata'][] = $line;
		return $this;
	}

	/**
	 * Adds a meta line
	 * 
	 * @access public
	 * @param string $name 
	 * @param string $content
	 * @return Template 
	 */
	public function meta($name, $content)
	{
		$this->_data['metadata'][] = "<meta name='{$name}' content='$content'/>";
		return $this;
	}

	/**
	 * Allows setting modules for the layout
	 * See Wiki
	 * 
	 * @access public
	 * @param string $location | The location you wish to set
	 * @param string $module | Module Name
	 * @param array $data | Passed data to the module
	 * @return Template 
	 */
	public function module($location, $module, $data = array())
	{
		// Adds to an array based off the location
		if(!is_array($module))
		{
			$this->_ci->load->vars($location, Modules::run($module, $data));
		}
		else
		{
			foreach($module as $m => $d)
			{
				$loaded_modules[] = Modules::run($m, $d);
			}
			
			$this->_ci->load->vars($location, $loaded_modules);
			
		}
		
		return $this;
	}

	/**
	 * Allows loading of a partials file
	 * 
	 * @access public
	 * @param string $partial_name | The name to reference in the layout
	 * @param string $partial_file | The name of the /partials/ file
	 * @param array $partial_data | Array of data to pass to partial
	 * @return Template 
	 */
	public function partial($partial_name, $partial_file, $partial_data = array())
	{
		//$this->_partials[$partial_name] = $partial_file;
		$this->_partials[] = array(
			'name' => $partial_name,
			'file' => $partial_file,
			'data' => $partial_data
		);
		return $this;
	}

	/**
	 * Overides the default layout
	 * 
	 * @access public
	 * @param string $layout | Layout file name
	 * @return Template 
	 */
	public function layout($layout)
	{
		
		$this->_layout = $layout;
		return $this;
	}

	/**
	 * Overides the default theme
	 * 
	 * @access public
	 * @param type $template
	 * @return Template 
	 */
	public function change($template)
	{
		$this->_template = $template;
		return $this;
	}

	/**
	 * Allows overiding the default view assumption
	 * 
	 * @access public
	 * @param string $view | The view file location
	 * @return Template 
	 */
	public function view($view)
	{
		$this->_view = $view;
		return $this;
	}

	/**
	 * Builds the template.  First builds the view, 
	 * then sends the view to the layout
	 * 
	 * @access public
	 * @param string $view | View file
	 * @param bool $return | If you want it wrapped in the layout
	 * @return void
	 */
	public function render($data = array())
	{
		// Merges data set and any additional data passed in
		$this->_data = array_merge($this->_data, $data);

		// Set the view variables
		$this->_ci->load->vars($this->_data);

		// Gets the view file
		
		$this->_layout_data['body'] = $this->_ci->load->view($this->_view, '', TRUE);
		
		// Load partial views
		foreach($this->_partials as $partial)
		{
			$this->_layout_data['partials'][$partial['name']] = $this->_ci->load->view("templates/{$this->_template}/{$this->_partials_folder}/{$partial['file']}", $partial['data'], TRUE);
		}

		// Load the view
		$this->_ci->load->view("templates/{$this->_template}/{$this->_layouts_folder}/{$this->_layout}", $this->_layout_data,true);
	}
	
	public function body($view = NULL, $return = FALSE)
    {
        // If no view line passed
        if(!$view)
        {
            // The view file
            $view = "{$this->_module}/{$this->_template}/{$this->_controller}-{$this->_method}";
        }

        // Set the view variables
        $this->_ci->load->vars($this->_data);
        
        // Gets the view file
        $this->_layout_data['body'] = $this->_ci->load->view($view, '', TRUE);

        // Return view if requested
        if($return)
            return $this->_layout_data['body'];

        // Load partial views
        $this->_load_partials();

        // Load the view
        $this->_ci->load->view("templates/{$this->_template}/{$this->_layout}", $this->_layout_data);
    }

}

/* End of file template.php */
/* Location: application/libraries/template.php */